<template>
  <div id="header">
    <div class="left">
      <div class="currentCity">
        <i class='iconfont metro-dingwei' style="color:#eb9a02;"></i>
        <div class="cityname">{{ cityname }}</div>
      </div>
      <div class="currentDate">{{ currentDate }}</div>
      <div class="currentWeek">{{ currentWeek }}</div>
      <div class="currentTime">{{ currentTime }}</div>
    </div>
    <div class="title">地铁三维可视化管控平台</div>
    <div class="right">
      <div class="currentWeather">
        <i :class="['iconfont', weatherIcon, 'icon']"></i>
        <div class="weather">{{ weather }}</div>
      </div>

      <div class="temperature">{{ temperature }}℃</div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import { getWeather } from '@/api/line.js';

const cityname = ref('武汉市')
const currentDate = ref('')
const currentWeek = ref('')
const currentTime = ref('')

const weatherIcon = ref('')
const weather = ref('')
const temperature = ref('')

const weekMap = [
  '星期日',
  '星期一',
  '星期二',
  '星期三',
  '星期四',
  '星期五',
  '星期六',
]

const weatherMap = {
  '晴': 'icon-qing',
  '雨': 'icon-xiaoyu',
  '雪': 'icon-xiaoxue',
  '多云': 'icon-yin',
  '阴': 'icon-yin',
  '雾': 'icon-wu',
  '霾': 'icon-wu'
}

onMounted(async () => {
  // 格式化日期
  currentDate.value = formatDate()
  currentWeek.value = weekMap[formatWeek()]

  setInterval(() => {
    currentTime.value = formatTime()
  }, 1000)

  // 请求天气接口
  const data = await getWeather()

  weatherIcon.value = weatherMap[data.lives[0].weather]
  weather.value = data.lives[0].weather
  
  setInterval(() => {
    weather.value = data.lives[0].weather
    
  }, 300000)
  
  temperature.value = data.lives[0].temperature
})

function formatDate() {
  const date = new Date()
  const year = date.getFullYear();
  const month = String(date.getMonth() + 1).padStart(2, '0'); // getMonth() returns 0-11
  const day = String(date.getDate()).padStart(2, '0');

  return `${year}/${month}/${day}`;
}
function formatTime() {
  const date = new Date()
  let hours = date.getHours(); // 获取当前小时 (0-23)
  let minutes = date.getMinutes(); // 获取当前分钟 (0-59)
  let seconds = date.getSeconds(); // 获取当前秒数 (0-59)

  // 确保时间为两位数，如果是个位数则前面加 '0'
  hours = ('0' + hours).slice(-2);
  minutes = ('0' + minutes).slice(-2);
  seconds = ('0' + seconds).slice(-2);

  return `${hours}:${minutes}:${seconds}`;
}
function formatWeek() {
  const date = new Date()
  const week = date.getDay()

  return week
}
</script>

<style scoped lang="scss">
@import url('http://at.alicdn.com/t/c/font_4778950_jht8cywqcuq.css');

#header {
  width: 100%;
  height: 57px;
  position: absolute;
  top: 0;
  left: 0;
  background: url('../assets/uiResources/header.png');
  background-size: cover;
  display: flex;
  justify-content: center;
  z-index: 999;

  .left {
    width: 26%;
    height: 70%;
    position: absolute;
    left: 0;
    top: 0;
    display: flex;
    align-items: center;
    gap: 30px;
    color: #fff;
    padding-left: 20px;
    font-size: 15px;

    .currentCity {
      display: flex;
      gap: 5px;
    }
  }

  // title字体的渐变
  .title {
    font-family: '等线Bold';
    font-size: 26px;
    font-weight: bold;
    font-stretch: normal;
    // constter-spacing: 2px;
    background: rgb(255, 255, 255);
    background-image: linear-gradient(180deg, rgba(255, 255, 255, 1) 9%, rgba(211, 156, 50, 1) 57%);
    background-clip: text;
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    margin-top: 3px;
  }

  .right {
    width: 26%;
    height: 70%;
    position: absolute;
    right: 0;
    top: 0;
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: 30px;
    font-size: 12px;
    color: #fff;
    margin-right: 20px;

    .currentWeather {
      display: flex;
      gap: 5px;
    }
  }

  .icon {
    font-family: '等线Bold';
    font-size: 16px;
    font-weight: bold;
    font-stretch: normal;
    background: rgb(255, 255, 255);
    background-image: linear-gradient(180deg, rgba(255, 255, 255, 1) 9%, rgba(211, 156, 50, 1) 57%);
    background-clip: text;
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    margin-right: 10px;
  }
}
</style>